﻿// ....................................................................................................................
//	Author: Copyright (C) 2012 Ing. Marek Hubal
//	Email: hubal@acasoftware.org
// ....................................................................................................................

using System;

namespace core
{
    /// <summary>Trieda IFilterStorage</summary>
	public class IFilterStorage : IDisposable
	{
        protected System.Collections.Generic.List<string> FItems; // Zoznam

        /// <summary>Property: Count</summary>
        public int Count
        {
            get
            {
                return FItems.Count;
            }
        }

        /// <summary>Ziskanie nazvu filtra</summary>
        /// <param name="pIndex">pozicia</param>
        /// <returns>nazov</returns>
        public string GetName(int pIndex)
        {
            return ((pIndex < 0) || (pIndex >= FItems.Count)) ? "" : ICore.GetSubstring(FItems[pIndex], 0, (char)0x1);
        }
        /// <summary>Ziskanie hodnoty filtra</summary>
        /// <param name="pIndex">pozicia</param>
        /// <returns>nazov</returns>
        public string GetValue(int pIndex)
        {
            return ((pIndex < 0) || (pIndex >= FItems.Count)) ? "" : ICore.GetSubstring(FItems[pIndex], 1, (char)0x1);
        }

        /// <summary>Konstruktor</summary>
        public IFilterStorage()
        {
            FItems = new System.Collections.Generic.List<string>();
        }
        /// <summary>Dispose metoda</summary>
        public void Dispose()
        {
            if (FItems != null)
            {
                FItems.Clear();
                FItems = null;
            }
        }

        /// <summary>Vymazanie obsahu</summary>
        public void Clear()
        {
            FItems.Clear(); // Vymazem zoznam
        }
        /// <summary>Zaradenie filtra</summary>
        /// <param name="pName">nazov</param>
        /// <param name="pValue">hodnota</param>
        public void Add(string pName, string pValue)
        {
            FItems.Add(pName + ((char)0x1).ToString() + pValue); // Spojim a zaradim
        }
        /// <summary>Ulozenie novej hodnoty</summary>
        /// <param name="pIndex">pozicia</param>
        /// <param name="pValue">nova hodnota</param>
        public void ReplaceValue(int pIndex, string pValue)
        {
            if ((pIndex >= 0) && (pIndex < FItems.Count)) {
                FItems[pIndex] = GetName(pIndex) + ((char)0x1).ToString() + pValue; // Ulozim
            }
        }
        /// <summary>Vymazanie zaznamu</summary>
        /// <param name="pIndex">pozicia</param>
        public void Delete(int pIndex)
        {
            if ((pIndex >= 0) && (pIndex < FItems.Count)) FItems.RemoveAt(pIndex);
        }
     
        /// <summary>Serializacia obsahu</summary>
        /// <param name="pEncode">priznak zakodovania</param>
        /// <returns>vysledok</returns>
        public string Serialize(bool pEncode = true)
        {
            System.Text.StringBuilder B = new System.Text.StringBuilder();
            foreach (string S in FItems)
            {
                B.Append(S); B.Append(Environment.NewLine);
            }
            return pEncode ? ICore.Base64Encode(B.ToString()) : B.ToString();
        }
        /// <summary>Deserializacia obsahu</summary>
        /// <param name="pData">serializovany obsah</param>
        /// <param name="pDecode">priznak dekodovania</param>
        public void Deserialize(string pData, bool pDecode = true)
        {
            try
            {
                string Data = pDecode ? ICore.Base64Decode(pData) : pData;
                using (System.IO.StringReader R = new System.IO.StringReader(Data)) // String reader
                {
                    while (true)
                    {
                        string Line = R.ReadLine();
                        if (Line == null) break; // Koniec?
                        if (string.IsNullOrWhiteSpace(Line)) continue; // Prazdny riadok?

                        FItems.Add(Line); // Zaradim
                    }
                }
            }
            catch // Chyba
            {
                FItems.Clear();
            }
        }
    }
}
